package com.fjwt.gz.service.activity.platform.acte019;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.cache.RedisUtil;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.bo.ActivityAwardAndSettingBo;
import com.fjwt.gz.db.bo.ActivityAwardBO;
import com.fjwt.gz.db.bo.ActivityUserTimesBO;
import com.fjwt.gz.db.dto.FansActivityDTO;
import com.fjwt.gz.db.entity.ActivityEntity;
import com.fjwt.gz.db.entity.AgentInfoEntity;
import com.fjwt.gz.db.entity.FansAuthEntity;
import com.fjwt.gz.db.vo.ActivityAwardVO;
import com.fjwt.gz.db.vo.ExportAwardVO;
import com.fjwt.gz.service.activity.BaseActivityDTO;
import com.fjwt.gz.service.activity.BaseActivityService;
import com.fjwt.gz.service.impl.*;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.OutputStream;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 引导用户实名认证活动------
 *
 */
@Slf4j
@Service
public class ACTE019Service extends BaseActivityService<Object> {

    private static String logPrefix = "【引导用户实名认证】";
    @Autowired
    private ActivityAwardService activityAwardService;
    @Autowired
    private SendAwardService sendAwardService;
    @Autowired
    private ActivitySettingService activitySettingService;
    @Autowired
    private FansAwardService fansAwardService;
    @Autowired
    private AwardSettingService awardSettingService;
    @Autowired
    private FansAuthService fansAuthService;
    @Autowired
    private AgentInfoService agentInfoService;

    /**
     * 新建活动
     */
    @Override
    public void add(BaseActivityDTO activityDTO) {
        log.info("新增ACTE019的活动");
        super.add(activityDTO);
    }

    /**
     * 修改活动
     */
    @Override
    public void update(BaseActivityDTO activityDTO) {
        log.info("更新ACTE019的活动");
        super.update(activityDTO);
    }

    /**
     * 删除活动
     */
    @Override
    public void delete(Long activityId) {
        log.info("删除ACTE019的活动");
        super.delete(activityId);
    }

    /**
     * 活动详情--pc
     */
    @Override
    public BaseActivityDTO detail(Long activityId) {
        log.info("查看ACTE019的活动详情");
        BaseActivityDTO baseActivityDetailsVO = super.detail(activityId);
        return baseActivityDetailsVO;
    }

    /**
     * 参与活动
     */
    @Override
    public ActivityAwardVO takePartIn(FansActivityDTO dto) {
        log.info("进入引导用户实名认证活动");
        super.takePartIn(dto);

        AgentInfoEntity agentInfoEntity = agentInfoService.getAgentInfo(dto.getLocationAgentNo());

        // 查询用户实名信息
        FansAuthEntity fansAuthEntity = fansAuthService.getOne(FansAuthEntity.gw()
                .eq(FansAuthEntity::getAgentNo, dto.getLocationAgentNo())
                .eq(FansAuthEntity::getFansId, dto.getFansId()));

        // 判断用户是否实名
        if (ObjectUtils.isNotEmpty(fansAuthEntity) && ObjectUtils.isNotEmpty(fansAuthEntity.getCstmNo())) {
            throw new BizException("您已是郑州邮政实名会员，请前往“郑邮享”小程序，享受更多会员权益。");
        }

        // 检测用户参与限制条件
        JSONObject jsonObject = activitySettingService.checkActivitySetting(fansEntity, dto.getAppId(), redisOutTime, activityEntity);

        // 判断用户是否实名
        if (ObjectUtils.isEmpty(fansAuthEntity) || ObjectUtils.isEmpty(fansAuthEntity.getCstmNo())) {
            boolean isJoinRealNameAuth = fansAwardService.isJoinRealNameAuth(dto.getFansId(), dto.getLocationAgentNo());
            if (isJoinRealNameAuth) {
                throw new BizException("您已参加过同类型的实名认证活动");
            }
        }

        // 检测活动奖品配置
        List<ActivityAwardAndSettingBo> list = awardSettingService.getActivityAwardList(dto.getActivityId(), redisOutTime);
        List<ActivityAwardBO> activityAwardBOList = activityAwardService.getAwardList(list);
        if (CollUtil.isEmpty(activityAwardBOList)) {
            throw new BizException("当前活动无可用奖品");
        }
        ActivityAwardBO activityAwardBO = activityAwardBOList.get(0);
        log.info("活动奖品详细信息:{}", JSONUtil.toJsonStr(activityAwardBO));
        long fansAwardId = sendAwardService.sendActivateAward(dto.getFansId(),dto.getAppId(),activityAwardBO.getAwardId(),
                Constants.AWARD.COUPON, activityEntity.getEndTime(), logPrefix,null,
                Constants.FANS_AWARD_TAG.REAL_NAME_AUTH,"{\"templateNo\":\"ACTE019\"}", agentInfoEntity);
        activityAwardBO.setFansAwardId(fansAwardId);

        //更新Redis中参与的次数
        ActivityUserTimesBO activityUserTimesBO = jsonObject.getObject("activityUserTimesBO", ActivityUserTimesBO.class);
        String currentTimesKey = jsonObject.getString("currentTimesKey");
        if (ObjectUtils.isNotEmpty(activityUserTimesBO)) {
            activitySettingService.updateRedisParticipateNum(currentTimesKey, activityUserTimesBO, redisOutTime);
        }
        Boolean userJoinScoreValue = jsonObject.getObject("userJoinScoreValue", Boolean.class);
        if (ObjectUtils.isNotEmpty(userJoinScoreValue)){
            RedisUtil.set(jsonObject.getString("userJoinScoreKey"), userJoinScoreValue, redisOutTime, TimeUnit.MILLISECONDS);
        }
        return activityAwardService.toUserData(activityAwardBO);
    }

    @Override
    public void realNameAfter(JSONObject jsonObject) {
        log.info("{}，参数信息：{}",logPrefix,jsonObject.toJSONString());
        Long fansId = jsonObject.getLong("fansId");
        Long activityId = jsonObject.getLong("activityId");
        String appId = jsonObject.getString("appId");
        FansActivityDTO dto = new FansActivityDTO();
        dto.setFansId(fansId);
        dto.setActivityId(activityId);
        dto.setAppId(appId);
        takePartIn(dto);
    }
}
